﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
/// <summary>
/// 玩家数据组件
/// </summary>

public class PlayerCom : ECSMomentCompoentBase
{
    /// <summary>
    /// 玩家名
    /// </summary>
    public string nickName;
    /// <summary>
    /// 角色id
    /// </summary>
    public string characterID;
    /// <summary>
    /// 得分
    /// </summary>
    public int score = 0;
    /// <summary>
    /// 玩家基本数据
    /// </summary>
    private PlayerDataGenerate characterData;
    /// <summary>
    /// 玩家朝向
    /// </summary>
    public SyncVector3 faceDir=new SyncVector3();
    /// <summary>
    /// 基本数据列表
    /// </summary>
    public List<ElementData> elementDatas=new List<ElementData>();
    /// <summary>
    /// buff列表
    /// </summary>
    public List<BuffInfo> buffList=new List<BuffInfo>();
    public override ECSMomentCompoentBase DeepCopy()
    {
        int go = elementDatas.Count;
        int co = buffList.Count;
        if (go==0&&co==0)
        {
            return null;
        }
       PlayerCom playerCom=new PlayerCom();
       playerCom.faceDir = faceDir;
       playerCom.characterID = characterID;
       playerCom.nickName = nickName;
       playerCom.score = score;

       for (int i = 0; i < go; i++)
       {
           playerCom.elementDatas.Add(elementDatas[i].DeepCopy());
       }

       for (int i = 0; i < co; i++)
       {
           playerCom.buffList.Add(buffList[i].DeepCopy());
       }

       return playerCom;
    }


    /// <summary>
    /// 添加基本数据
    /// </summary>
    /// <param name="elementID"></param>
    public void AddElement(int elementID)
    {
        for (int i = 0,j=elementDatas.Count; i < j; i++)
        {
            if (elementDatas[i].id.Equals(elementID))
            {
                elementDatas[i].num++;
            }
        }
    }
    /// <summary>
    /// 添加buff
    /// </summary>
    /// <param name="buffID"></param>
    /// <param name="creater"></param>
    /// <returns></returns>
    public BuffInfo AddBuff(string buffID, int creater)
    {
        BuffInfo go;
        for (int i = 0,j=buffList.Count; i < j; i++)
        {
            go = buffList[i];
            if (go.buffID.Equals(buffID) && 
                 go.creater.Equals(creater))
            {
                go.buffCount++;
                go.BuffTime = 0;

                return go;
            }
        }
        go=new BuffInfo();
        go.buffID = buffID;
        go.creater = creater;

        buffList.Add(go);
        return go;
    }
    /// <summary>
    /// 获取玩家速度
    /// </summary>
    /// <returns></returns>
    public int GetSpeed()
    {
        float speed = characterData.m_movespeed;
        int go = buffList.Count;
        BuffDataGendrate thisBuffData;
        if (go !=0)
        {
            float changeNumber = 0;
            float changePercantage = 1;
            for (int i = 0; i < go; i++)
            {
                thisBuffData = buffList[i].BuffData;
                changeNumber += thisBuffData.m_SpeedChange;
                changePercantage *= thisBuffData.m_SpeedChangePercentage;
            }

            speed *= changePercantage;
            speed += changeNumber;
            thisBuffData = null;
            
        }

        return (int) (speed * 1000);
    }
    /// <summary>
    /// 是否呗晕眩
    /// </summary>
    /// <returns></returns>
    public bool GetISDizziness()
    {
        int go = buffList.Count;
        if (go.Equals(0))
        {
            return false;
        }

        for (int i = 0; i < go; i++)
        {
            if (buffList[i].BuffData.m_Dizziness)
            {
                return true;
            }
        }

        return false;
    }
  
}
/// <summary>
/// 基本数据
/// </summary>
public class ElementData
{
    /// <summary>
    /// 唯一id
    /// </summary>
    public int id;
    /// <summary>
    /// 相同id清空自增num
    /// </summary>
    public int num;

    public ElementData DeepCopy()
    {
        ElementData ed=new ElementData();
        ed.id = id;
        ed.num = num;
        return ed;
    }
}
/// <summary>
/// 角色已有Buff信息
/// </summary>
public class BuffInfo
{
    /// <summary>
    /// buff数据路径
    /// </summary>
    public string buffID;
    /// <summary>
    /// 创建者
    /// </summary>
    public int creater;
    /// <summary>
    /// 已持续时间
    /// </summary>
    public int BuffTime;
    /// <summary>
    /// buff数目
    /// </summary>
    public int buffCount;
    /// <summary>
    /// 受击时间
    /// </summary>
    public int hitTime;
    /// <summary>
    /// buff数据
    /// </summary>
    private BuffDataGendrate buffData;

    public BuffDataGendrate BuffData
    {
        get {
            if (buffData.Equals(null))
            {
                buffData = DataGenerateManager<BuffDataGendrate>.GetData(buffID);
            }

            return buffData;
        }
    }

    public BuffInfo DeepCopy()
    {
        BuffInfo bi=new BuffInfo();

        bi.buffID = buffID;
        bi.creater = creater;
        bi.BuffTime = BuffTime;
        bi.buffCount = buffCount;
        bi.hitTime = hitTime;

        return bi;
    }
}
